/**
 * desc :
 * User : hjt
 * Date : 2017/1/20
 */
var http = require("http");
var fs = require("fs");

//读取配置文件
var httpJsonString = fs.readFileSync(__dirname + "/config/http.json", "utf8");
var httpJson = JSON.parse(httpJsonString);

http.createServer(function(req, res){
    //解析路径,规范化url,去掉查询字符串，可选的反斜线，并且把它变成小写方便处理。
    //你访问 /course/main =-->/course/main
    ///COURSE/MAIN-->/course/main
    var path = req.url.replace(/\/?(?:\?.*)?$/,"").toLowerCase();
    switch (path) {
        case "" :
            res.writeHead(200, {"Content-Type" : "text/html"});
            res.end("<h1>这是首页</h1>");
            break;
        case "/about" :
            res.writeHead(200, {"Content-Type" : "text/plain"});
            res.end("关于我们");
            break;
        case "/home" :
            res.writeHead(200,{"Content-Type" : "text/plain"});
            res.end("home页面");
            break;
    }
}).listen(httpJson.port, httpJson.hostname);
console.log("你当前服务器已经启动：端口是:"+httpJson.port+",访问地址是：http://:"+httpJson.hostname+":"+httpJson.port);
console.log("停止服务用：ctrl+c ");